fastlane_version '2.154.0'
import "./Android.Fastfile"
import "./iOS.Fastfile"

before_all do |lane|
  if lane != :get_latest_version
    # Pull all tags
    git_pull(only_tags: true)

    # Run yarn install
    yarn(
      command: 'install'
    )
  end
end

lane :load_env_file do |options|
  buildType = options[:buildType]

  # Make sure required env file exists
  env_file=".env.#{buildType}"
  ensure_env_file_exists(file: env_file)

  env_data = File.read(File.expand_path "../#{env_file}")
  UI.message("==============================================================")
  UI.message("----- ENV FILE: #{File.expand_path "../#{env_file}"}")
  UI.message("==============================================================")
  UI.message(env_data)
  UI.message("==============================================================")

  # Load the environment
  Dotenv.overload "../#{env_file}"
  ENV["ENVFILE"] = env_file
end

lane :onboard do
  match(
    type: "development",
    app_identifier: "ca.gc.hcsc.canada.stopcovid",
    readonly: true,
  )
  match(
    type: "adhoc",
    app_identifier: "ca.gc.hcsc.canada.covidalert.dev",
    readonly: true,
  )
  match(
    type: "appstore",
    app_identifier: "ca.gc.hcsc.canada.stopcovid",
    readonly: true,
  )
  match(
    type: "appstore",
    app_identifier: "ca.gc.hcsc.canada.covidalert.dev",
    readonly: true,
  )
end

lane :onboard_dev do
  match(
    type: "development",
    app_identifier: "ca.gc.hcsc.canada.stopcovid",
    readonly: true,
  )
end

desc "Get latest version number"
lane :get_latest_version do
  primary_play_versions = google_play_track_version_codes(
    track: 'internal',
    package_name: 'ca.gc.hcsc.canada.stopcovid'
  )

  secondary_play_versions = google_play_track_version_codes(
    track: 'internal',
    package_name: 'ca.gc.hcsc.canada.covidalert.dev'
  )

  versions = Array(primary_play_versions) + Array(secondary_play_versions)

  primary_testflight_latest = latest_testflight_build_number(
    app_identifier: 'ca.gc.hcsc.canada.stopcovid'
  )

  secondary_testflight_latest = latest_testflight_build_number(
    app_identifier: 'ca.gc.hcsc.canada.covidalert.dev'
  )

  versions = versions + Array(primary_testflight_latest) + Array(secondary_testflight_latest)

  UI.success('Last used version code: ' + String(versions.max))
end

desc "Creates a Github Release"
lane :create_github_release do |options|
  UI.user_error!("platform is missing") unless options[:platform]
  upload_assets = options[:upload_assets] ? options[:upload_assets] : []

  set_github_release(
    repository_name: "cds-snc/covid-alert-app",
    api_token: ENV["GITHUB_TOKEN"],
    name: "#{options[:platform]}  v#{ENV['APP_VERSION_NAME']} (#{ENV['APP_VERSION_CODE']})",
    tag_name: "#{options[:platform]}-v#{ENV['APP_VERSION_NAME']}-#{ENV['APP_VERSION_CODE']}",

    description: default_changelog,
    commitish: "master",
    upload_assets: upload_assets
  )
end

desc "Returns a default changelog."
lane :default_changelog do |options|
  format = (options[:simple] ? "- %b" : "- %b [%s]")

  changelog = changelog_from_git_commits(
    merge_commit_filtering: "only_include_merges",
    pretty: format
  ), Actions.lane_context[SharedValues::FL_CHANGELOG] = changelog

  clean = changelog.first.gsub("- \n", "")
  puts clean
  clean
end

lane :ensure_build_directory do
  UI.message("Checking for build directories")

  unless File.directory?('../build/ios')
    FileUtils.mkdir_p('../build/ios')
  end
  unless File.directory?('../build/android')
    FileUtils.mkdir_p('../build/android')
  end
end

lane :ensure_env_file_exists do |options|
  file = options[:file]
  file_exists = File.exist? File.expand_path "../#{file}"

  UI.user_error!("#{file} environment file is missing!") unless file_exists
  UI.success("Using environment file #{file}")
end

lane :ensure_translations_generated do
  yarn(
    command: "generate-translations"
  )

  translations_status = Actions.sh("git status ../src/locale/translations --porcelain")

  repo_clean = translations_status.empty?

  UI.user_error!("Ok, go commit that file!") unless prompt(
    text: "The translations file appears to be dirty, and changes should be committed. Are you sure you want to continue?",
    boolean: true
  ) unless repo_clean
end
